package com.photoeditor.demo.model.store.module;

import android.text.TextUtils;

import com.common.base.ComponentContext;
import com.common.base.utils.FileUtil;
import com.photoeditor.R;
import com.photoeditor.demo.app.AppApplication;
import com.photoeditor.demo.util.ImageCache;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 *
 * Created by edward on 17-7-14.
 */

public class LocalPluginUtil {
    /**
     * sticker插件包本地文件夹
     */
    public static final String STICKER_PLUGIN_FOLDER_PATH = FileUtil
            .getExternalCacheDir(ComponentContext.getContext(), "stickerzip", true);
    public static final String[] INSIDE_STICKER_PKGNAME = {
            "com.photolab.sticker.sunglasses",
    };

    public static final int[] INSIDE_STICKER_ID = {R.raw.sunglass};


    /**
     * 生成用于本地存放的文件名
     * @param pkgName 插件的包名
     * @return*/

    public static String generateLocalFilePath(String basePath, String pkgName) {
        return basePath + "/" + ImageCache.hashKeyForDisk(pkgName) + ".zip";
    }

    public static boolean isPkgFileExist(String basePath, String pkgName) {
        return FileUtil.isExistFile(generateLocalFilePath(basePath, pkgName));
    }

    /**
     * 是否内置包
     * @param pkgName
     * @return
     */
    public static boolean isInsidePkg(String pkgName) {
        for (String pkg : INSIDE_STICKER_PKGNAME) {
            if (TextUtils.equals(pkgName, pkg)) {
                return true;
            }
        }
        return false;
    }

    public static void copyInsideFileToExternalPath() {
        AppApplication appApplication = AppApplication.getApplication();

        for (int i = 0; i < INSIDE_STICKER_PKGNAME.length; i++) {
            File file = new File(generateLocalFilePath(STICKER_PLUGIN_FOLDER_PATH, INSIDE_STICKER_PKGNAME[i]));
            try {
                InputStream inputStream = appApplication.getResources().openRawResource(INSIDE_STICKER_ID[i]);
                FileOutputStream fileOutputStream = new FileOutputStream(file);
                byte buf[]=new byte[8192];
                int len;
                while((len=inputStream.read(buf))>0) {
                    fileOutputStream.write(buf,0,len);
                }

                fileOutputStream.close();
                inputStream.close();
            } catch (IOException e1) {
            }
        }
    }
    public static boolean copyInsideFileToExternalPath(String pkgName) {
        AppApplication appApplication = AppApplication.getApplication();

        boolean success = true;
        for (int i = 0; i < INSIDE_STICKER_PKGNAME.length; i++) {
            if (!TextUtils.equals(pkgName, INSIDE_STICKER_PKGNAME[i])) {
                continue;
            }
            File file = new File(generateLocalFilePath(STICKER_PLUGIN_FOLDER_PATH, INSIDE_STICKER_PKGNAME[i]));
            try {
                InputStream inputStream = appApplication.getResources().openRawResource(INSIDE_STICKER_ID[i]);
                FileOutputStream fileOutputStream = new FileOutputStream(file);
                byte buf[]=new byte[8192];
                int len;
                while((len=inputStream.read(buf))>0) {
                    fileOutputStream.write(buf,0,len);
                }

                fileOutputStream.close();
                inputStream.close();
            } catch (IOException e1) {
                success = false;
            }
        }
        return success;
    }
}
